package com.niiwoo.civet.account.service.local.fundsFreeze;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.account.dao.entity.FundsFreezeOrder;
import com.niiwoo.civet.account.dao.entity.FundsUnfreezeOrder;
import com.niiwoo.civet.account.dto.bill.AddBillAndChangeBalanceDTO;
import com.niiwoo.civet.account.dto.bill.AddBillDetailDTO;
import com.niiwoo.civet.account.enums.*;
import com.niiwoo.civet.account.service.local.bill.BillFacade;
import com.niiwoo.civet.base.enums.ChannelCodeEnum;
import com.niiwoo.tripod.provider.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Jaylin
 * @Date 2018-04-27
 * @Descript 资金冻结事务服务类
 */
@Service
@Slf4j
public class FundsFreezeTransactionService {

    @Autowired
    private FundsFreezeOrderService freezeOrderService;
    @Autowired
    private BillFacade billFacade;
    @Autowired
    private FundsUnfreezeOrderService unfreezeOrderService;

    /**
     * 更新冻结成功的订单状态
     *
     * @param id
     */
    @Transactional
    public void dealFreezeOrderSuccess(Long id) {
        FundsFreezeOrder freezeOrder = freezeOrderService.selectByPrimaryKeyForUpdate(id);
        if (freezeOrder.getStatus() == FundsFreezeOrderStatusEnum.STATUS_SUCCESS.getStatus()) {
            log.error("订单已经是成功状态,无需再次进行订单处理");
            return;
        }

        if (freezeOrder.getStatus() == FundsFreezeOrderStatusEnum.STATUS_FAIL.getStatus()) {
            log.error("数据库存储订单状态与懒猫返回的状态不一致,数据出现异常");
            return;
        }

        if (freezeOrder.getStatus() == FundsFreezeOrderStatusEnum.STATUS_DEAL.getStatus()) {

            freezeOrderService.updateFreezeOrderSuccess(freezeOrder.getId());

            AddBillAndChangeBalanceDTO billDTO = new AddBillAndChangeBalanceDTO();
            billDTO.setAccountId(freezeOrder.getAccountId());
            billDTO.setAmount(freezeOrder.getAmount());
            billDTO.setInoutType(InoutTypeEnum.OUT_TYPE);
            billDTO.setOrderId(freezeOrder.getOrderId());
            billDTO.setOrderType(OrderTypeEnum.FUNDS_FREEZE);
            billDTO.setRequestNo(freezeOrder.getRequestNo());
            billDTO.setChannelCode(ChannelCodeEnum.APP);
            billDTO.setStatus(BillStatusEnum.SUCCESS);
            billDTO.setAddBillDetailDTOList(new ArrayList<>());

            AddBillDetailDTO detailDTO = new AddBillDetailDTO();
            detailDTO.setAmount(freezeOrder.getAmount());
            detailDTO.setBillSecondSubjectEnum(this.getSecondEnum(freezeOrder.getOrderType(), InoutTypeEnum.OUT_TYPE));
            billDTO.getAddBillDetailDTOList().add(detailDTO);

            billFacade.addBillAndChangeBalance(billDTO);
        }
    }

    /**
     * 更新冻结失败的订单状态
     *
     * @param id
     * @return
     */
    @Transactional
    public void dealFreezeOrderFail(Long id, String errorCode, String errorMsg) {
        FundsFreezeOrder freezeOrder = freezeOrderService.selectByPrimaryKeyForUpdate(id);
        if (freezeOrder.getStatus() == FundsFreezeOrderStatusEnum.STATUS_SUCCESS.getStatus()) {
            log.error("数据库存储订单状态与懒猫返回的状态不一致,数据出现异常");
            return;
        }

        if (freezeOrder.getStatus() == FundsFreezeOrderStatusEnum.STATUS_FAIL.getStatus()) {
            log.error("数据库存储订单状态已经是失败状态,无需进行订单处理");
            return;
        }

        if (freezeOrder.getStatus() == FundsFreezeOrderStatusEnum.STATUS_DEAL.getStatus()) {
            freezeOrderService.updateFreezeOrderFail(id, errorCode, errorMsg);
        }
    }

    /**
     * 更新解冻成功的订单
     *
     * @param id
     */
    @Transactional
    public void dealUnfreezeOrderSuccess(Long id) {
        FundsUnfreezeOrder unfreezeOrder = unfreezeOrderService.selectByPrimaryKeyForUpdate(id);
        if (unfreezeOrder.getStatus() == FundsFreezeOrderStatusEnum.STATUS_SUCCESS.getStatus()) {
            log.error("订单已经是成功状态,无需再次进行订单处理");
            return;
        }

        if (unfreezeOrder.getStatus() == FundsFreezeOrderStatusEnum.STATUS_FAIL.getStatus()) {
            log.error("数据库存储订单状态与懒猫返回的状态不一致,数据出现异常");
            return;
        }

        if (unfreezeOrder.getStatus() == FundsFreezeOrderStatusEnum.STATUS_DEAL.getStatus()) {

            unfreezeOrderService.updateUnfreezeOrderSuccess(unfreezeOrder.getId());

            freezeOrderService.updateFundsFreezeOrderByRequestNo(unfreezeOrder.getFreezeRequestNo(), unfreezeOrder.getAmount());

            AddBillAndChangeBalanceDTO billDTO = new AddBillAndChangeBalanceDTO();
            billDTO.setAccountId(unfreezeOrder.getAccountId());
            billDTO.setAmount(unfreezeOrder.getAmount());
            billDTO.setInoutType(InoutTypeEnum.IN_TYPE);
            billDTO.setOrderId(unfreezeOrder.getOrderId());
            billDTO.setOrderType(OrderTypeEnum.FUNDS_FREEZE);
            billDTO.setRequestNo(unfreezeOrder.getRequestNo());
            billDTO.setChannelCode(ChannelCodeEnum.APP);
            billDTO.setStatus(BillStatusEnum.SUCCESS);
            billDTO.setAddBillDetailDTOList(new ArrayList<>());

            AddBillDetailDTO detailDTO = new AddBillDetailDTO();
            detailDTO.setAmount(unfreezeOrder.getAmount());
            detailDTO.setBillSecondSubjectEnum(this.getSecondEnum(unfreezeOrder.getOrderType(), InoutTypeEnum.IN_TYPE));
            billDTO.getAddBillDetailDTOList().add(detailDTO);

            billFacade.addBillAndChangeBalance(billDTO);
        }
    }

    public void dealUnfreezeOrderFail(Long id, String errorCode, String errorMsg) {
        FundsUnfreezeOrder unfreezeOrder = unfreezeOrderService.selectByPrimaryKeyForUpdate(id);
        if (unfreezeOrder.getStatus() == FundsFreezeOrderStatusEnum.STATUS_SUCCESS.getStatus()) {
            log.error("数据库存储订单状态与懒猫返回的状态不一致,数据出现异常");
            return;
        }

        if (unfreezeOrder.getStatus() == FundsFreezeOrderStatusEnum.STATUS_FAIL.getStatus()) {
            log.error("数据库存储订单状态已经是失败状态,无需进行订单处理");
            return;
        }

        if (unfreezeOrder.getStatus() == FundsFreezeOrderStatusEnum.STATUS_DEAL.getStatus()) {
            unfreezeOrderService.updateUnfreezeOrderFail(id,errorCode,errorMsg);
        }
    }

    /**
     * 根据订单类型获取对应的二级科目
     *
     * @param orderType
     * @return
     */
    private BillSecondSubjectEnum getSecondEnum(Byte orderType, InoutTypeEnum inoutTypeEnum) {
        BillSecondSubjectEnum billSecondSubjectEnum = null;
        //变更完状态记账
        if (InoutTypeEnum.IN_TYPE.getType().equals(inoutTypeEnum.getType())) {
            if (orderType == FundsOrderTypeEnum.TYPE_INSTITUTIONAL_DEPOSIT.getType()) {
                billSecondSubjectEnum = BillSecondSubjectEnum.COOPERATE_AGENCY_UNMARGIN_FEE;
            } else if (orderType == FundsOrderTypeEnum.TYPE_AGENCY_PERFORMANCE_INSURANCE.getType()) {
                billSecondSubjectEnum = BillSecondSubjectEnum.COOPERATE_AGENCY_UNINSURANCE_FEE;
            } else {
                throw new BizException("ACC7001");
            }
        } else if (InoutTypeEnum.OUT_TYPE.getType().equals(inoutTypeEnum.getType())) {
            if (orderType == FundsOrderTypeEnum.TYPE_INSTITUTIONAL_DEPOSIT.getType()) {
                billSecondSubjectEnum = BillSecondSubjectEnum.COOPERATE_AGENCY_MARGIN_FEE;
            } else if (orderType == FundsOrderTypeEnum.TYPE_AGENCY_PERFORMANCE_INSURANCE.getType()) {
                billSecondSubjectEnum = BillSecondSubjectEnum.COOPERATE_AGENCY_INSURANCE_FEE;
            } else {
                throw new BizException("ACC7001");
            }
        }

        return billSecondSubjectEnum;
    }


}
